/*
 * @Author: 我想吃余 483766615@qq.com
 * @Date: 2025-10-21 09:24:14
 * @LastEditors: 我想吃余 483766615@qq.com
 * @LastEditTime: 2025-10-22 10:27:50
 * @FilePath: /oscode/sha_mem/proc_a.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "comm.hpp"
#include "log.hpp"

int main()
{
    Init init;
    //创建共享内存
    int shmid = CreateShm();
    //挂接该进程
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);

    int fd = open(FIFO_FILE, O_RDONLY);
    if (fd < 0)
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }

    //共享内存属性集
    struct shmid_ds* shmds;
    //开始通信
    while(true)
    {
        char c;
        ssize_t s = read(fd, &c, 1);
        if(s <= 0) break;

        //直接访问共享内存
        cout<< "client say#" << shmaddr << endl;
        sleep(1);

        //查看共享内存的部分属性：
        shmctl(shmid, IPC_STAT, shmds);
        cout << "shm nattch" << shmds->shm_nattch<<endl;
        cout << "shm key" << shmds->shm_perm.__key<<endl;
    }
    //去挂接
    shmdt(shmaddr);
    //释放共享内存
    shmctl(shmid, IPC_RMID, nullptr);
    return 0;
}